package md.objects;

import md.constants.Constants;
import md.settings.Settings;

public class Enemy {

	// real object position
	public int realX;
	public int realY;
	
	// delta
	public int realDX = 256;
	public int realDY = 256;
	
	// visible position
	public int x;
	public int y;
	
	// state
	public boolean alive = false;
	
	//path
	public short[] path;
	public short pathPosition;
	
	public Enemy(int x, int y) {
		this(x, y, false);
	}
	
	public Enemy(int x, int y, boolean realPosition) {
		if(!realPosition) {
			x *= Settings.cellSize;
			y *= Settings.cellSize;
			
			x += Settings.cellSize >> 1;
			y += Settings.cellSize >> 1;
		}

		realX = calculateRealValueFromValue(x);
		realY = calculateRealValueFromValue(y);
		
		refreshPosition();
	}

	public final void calculateTurn() {
		if(!alive)
			return;
		
		realX += realDX;
		realY += realDY;
		refreshPosition();
	}
	
	public final void refreshPosition() {
		x = calculateValueFromRealValue(realX);
		y = calculateValueFromRealValue(realY);
	}
	
	public final static int calculateRealValueFromValue(int value) {
		return value << 8;
	}
	
	public final static int calculateValueFromRealValue(int realValue) {
		if(realValue % Constants.ENEMY_REAL_POSITION_FACTOR > Constants.ENEMY_REAL_POSITION_FACTOR_HALF)
			return (realValue >> 8) + 1;
		else
			return realValue >> 8;
	}
	
}
